﻿Imports System.Windows.Threading
Imports System.Collections.ObjectModel
Imports System.Threading.Tasks
Imports System.Threading

Namespace Statistics
    Public Class CustomerStats

        Private _dispatcher As Dispatcher
        Private _loader As IDatabase
        Private _usr As User

        Public Sub New(dp As Dispatcher, loader As IDatabase, usr As User)
            _dispatcher = dp
            _loader = loader
            _usr = usr
        End Sub

        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="customerid"></param>
        ''' <remarks></remarks>
        Public Sub GetMonthlySalesAsync(customerid As String, startdate As Date, enddate As Date)
            Dim t As Task = Task.Factory.StartNew(Sub()
                                                      GetMonthlySales(customerid, startdate, enddate)
                                                  End Sub)
        End Sub

        Public Event GetMonthlySalesCompleted(result As Collection(Of DateValueModel))
        Public Event GetMostBoughtArticlesCompleted(result As Collection(Of ArticleAmountModel))

        Private Sub GetMonthlySales(customerid As String, startdate As Date, enddate As Date)
            Dim output As New Collection(Of DateValueModel)

            '### Filter relevant orders ###'
            Dim orders As Collection(Of OrderPreviewModel) = _loader.GetOrdersByCustomer(customerid, _usr, startdate, enddate)

            '### Prepare output ###'
            Dim tempstart As Date = New Date(startdate.Year, startdate.Month, 1)
            Dim tempend As Date = enddate : enddate.AddMonths(1) : tempend = New Date(tempend.Year, tempend.Month, 1) : tempend.AddDays(-1)
            Dim currentdate As Date = tempstart
            Do While currentdate <= tempend
                Dim item As New DateValueModel
                Dim d As New Datum(currentdate)
                item.Date_ = d
                item.Value = New Money(0)
                output.Add(item)
                currentdate = currentdate.AddMonths(1)
            Loop

            '### Calcualte output ###'
            For Each o In orders
                Dim item As New DateValueModel
                Dim d As New Datum(1, New Datum(o.Date_).Monat, New Datum(o.Date_).Jahr)
                item.Date_ = d
                If output.Contains(item) Then
                    item = output.Item(output.IndexOf(item))
                    item.Value += o.Value()
                Else
                    item.Value = o.Value()
                    output.Add(item)
                End If
            Next

            _dispatcher.BeginInvoke(Sub()
                                        RaiseEvent GetMonthlySalesCompleted(output)
                                    End Sub)
        End Sub

        Private Sub DeleteItemsFromCollection(ByRef basecollection As Collection(Of Order), ItemsToDelete As Collection(Of Order))
            For Each item In ItemsToDelete
                basecollection.Remove(item)
            Next
        End Sub

        Public Sub GetMostBoughtArticlesAsync(customerid As String, startdate As Date, enddate As Date)
            Task.Factory.StartNew(Sub()
                                      GetMostBoughtArticles(customerid, startdate, enddate)
                                  End Sub)
        End Sub

        Private Sub GetMostBoughtArticles(customerid As String, startdate As Date, enddate As Date)
            Dim output As New List(Of ArticleAmountModel)

            '### Filter relevant orders ###'
            Dim orders As Collection(Of OrderPreviewModel) = _loader.GetOrdersByCustomer(customerid, _usr, startdate, enddate)
            Dim orderstodel As New Collection(Of Order)

            '### Calcualte ###'
            For Each o In orders
                Dim ord As Order = _loader.GetOrder(o.OrderID, o.OrderTyp, _usr)
                For Each a In ord.OrderedArticles
                    Dim item As New ArticleAmountModel(a, a.OrderedAmount, a.GrossOrderValue)
                    If output.Contains(item) Then
                        item = output.Item(output.IndexOf(item))
                        item.Amount += a.OrderedAmount
                        item.Proceeds += a.GrossOrderValue
                    Else
                        output.Add(item)
                    End If
                Next
            Next
            Dim sort As New ArticleAmountProceedsSort
            output.Sort(sort)

            Dim result As New Collection(Of ArticleAmountModel)
            For Each item In output
                result.Add(item)
            Next
            _dispatcher.BeginInvoke(Sub()
                                        RaiseEvent GetMostBoughtArticlesCompleted(result)
                                    End Sub)
        End Sub
    End Class
End Namespace

